package com.ce.cetag.tags.tab;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.ce.cetag.model.TabModel;

/**
 *
 * 功能描述：
 *
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序
 * </p>
 *
 * @author ��˧��贺帅彪
 * @create date : 2007-9-7
 */
public class TabStripTag extends TagSupport {

	private List<TabModel> tabList = new ArrayList<TabModel>();

	private StringBuffer sb = new StringBuffer();

	private StringBuffer tabContent = new StringBuffer();

	private StringBuffer tabItem = new StringBuffer();

	private String selectedTab;

	private String isMouseOver;

	private String onClick;

	public String getOnClick() {
		return onClick;
	}

	public void setOnClick(String callback) {
		this.onClick = callback;
	}

	public String getIsMouseOver() {
		return isMouseOver;
	}

	public void setIsMouseOver(String isMouseOver) {
		this.isMouseOver = isMouseOver;
	}

	public String getSelectedTab() {
		return selectedTab;
	}

	public void setSelectedTab(String selectedTab) {
		this.selectedTab = selectedTab;
	}

	public List<TabModel> getTabList() {
		return tabList;
	}

	@Override
	public int doStartTag() throws JspException {
		return super.EVAL_BODY_INCLUDE;
	}

	private void write(String str) {

		sb.append(str);
		sb.append("\n");
	}

	private void generateTabItem() {
		List<TabModel> list = getTabList();
		int i = 0;
		String fireEvent = null;
		if (getIsMouseOver() != null) {
			fireEvent = "onMouseOver";
		} else {
			fireEvent = "onClick";
		}

		for (Iterator<TabModel> it = list.iterator(); it.hasNext(); i++) {
			TabModel tabModel = it.next();
			String outEvent = tabModel.getOnClick() + ";" + getOnClick(); // tab
																			// 触发的事件
			outEvent = outEvent.replace("null", "");
			tabItem
					.append("<td><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"hand\">\n");
			tabItem.append("<tr>\n");
			tabItem.append("<td class=\"tab_offLeftBg\" id=\"tab_" + i
					+ "1\"></td>\n");
			tabItem.append("<td class=\"tab_offBg\" id=\"tab_" + i + "2\" "
					+ fireEvent + "=\"setBackground(" + i + ", '"
					+ tabModel.getId() + "');" + outEvent + "\" >");
			tabItem.append(tabModel.getLabel());
			tabItem.append("</td>\n");
			tabItem.append("<td class=\"tab_offRightBg\" id=\"tab_" + i
					+ "3\"></td>\n");
			tabItem.append("</tr>\n");
			tabItem.append("</table></td>\n");
			generateTabContent(tabModel);
		}

	}

	private String getTabItem() {
		return tabItem.toString();
	}

	private String getTabContent() {
		return tabContent.toString();
	}

	private void generateTabContent(TabModel tabModel) {

		tabContent.append(" <div style=\"display:none;\" id=\""
				+ tabModel.getId() + "\">\n");
		tabContent.append(tabModel.getContent());
		tabContent.append("</div>\n");
	}

	private void generateScript() {
		sb.append("<script language=\"JavaScript\" type=\"text/javascript\">");
		List<TabModel> tabList = getTabList();
		if (tabList.size() == 0) {
			return;
		}
		TabModel tabModel = null;
		if (getSelectedTab() == null) {
			tabModel = getTabList().get(0);
			sb.append("setBackground(0, \"" + tabModel.getId() + "\")");

		} else {
			int length = tabList.size();
			for (int i = 0; i < length; i++) {
				tabModel = tabList.get(i);
				if (tabModel.getId().equals(getSelectedTab())) {
					sb.append("setBackground(" + i + " ,\"" + tabModel.getId()
							+ "\")");
					break;
				}

			}

		}
		sb.append("</script>");
	}

	@Override
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		sb = new StringBuffer();

		write("<div class=\"tab_lineBg\">");
		write("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
		write("<tr>");

		write("<td><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
		write("<tr>");
		generateTabItem();
		write(getTabItem());

		write("</tr>");
		write("</table></td>");
		write("</tr>");
		write("</table>");
		write("</div>");

		write(getTabContent());
		generateScript();
		try {
			out.write(sb.toString());
			doRelease();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return EVAL_PAGE;
	}

	public void doRelease() {
		tabList.removeAll(tabList);
		sb.delete(0, sb.length());
		tabContent.delete(0, tabContent.length());
		tabItem.delete(0, tabItem.length());
	}
}
